package com.project.myxima.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.android.material.textfield.TextInputEditText;
import com.project.myxima.LoginActivity;
import com.project.myxima.MainActivity;
import com.project.myxima.R;
import com.project.myxima.base.BaseApplication;
import com.project.myxima.views.UILoader;

import java.util.Objects;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SignInFragment extends BaseFragment {
    private static final String TAG = "SignInFragment";
    private TextInputEditText mNameTv;
    private TextInputEditText mPasswordTv;
    private Button mSingInBt;
    private UILoader mUILoader;

    /*public SignInFragment() {
        // Required empty public constructor
    }*/


    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, ViewGroup container) {
        //this fangfa can not show UI
        /*View view = layoutInflater.inflate(R.layout.fragment_sign_in, container, false);
        initView(view);
        return view;*/

        /*mUILoader = new UILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return initView(layoutInflater, container);
            }
        };

        if(mUILoader.getParent() instanceof ViewGroup){
            ((ViewGroup) mUILoader.getParent()).removeView(mUILoader);
        }

        return mUILoader;*/

        return initView(layoutInflater, container);


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initEvent();
    }

    private void initEvent() {
        mSingInBt.setOnClickListener(v -> {
            BmobUser user = new BmobUser();
            String userName = Objects.requireNonNull(mNameTv.getText()).toString();
            String password = Objects.requireNonNull(mPasswordTv.getText()).toString();
            user.setUsername(userName);
            user.setPassword(password);
            boolean isEmpty = isEmpty(userName, password);
            if (!isEmpty) {
                synchronized (this) {
                    user.login(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if (e == null) {
                                Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                                LoginActivity loginActivity = (LoginActivity) getActivity();
                                startActivity(new Intent(loginActivity, MainActivity.class));
                                assert loginActivity != null;
                                loginActivity.finish();
                            } else {
                                Log.d(TAG, "errorMsg: " + e.getMessage());
                                Log.d(TAG, "errorCode: " + e.getErrorCode());
                                if (e.getErrorCode() == 101)
                                    Toast.makeText(BaseApplication.getAppContext(), "用户名或者密码错误 ！", Toast.LENGTH_SHORT).show();
                                else
                                    Toast.makeText(BaseApplication.getAppContext(), "网络错误，登录失败 ！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            } else {
                Toast.makeText(BaseApplication.getAppContext(), "用户名和密码不能为空 ！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private boolean isEmpty(String... strings) {
        for (String string : strings) {
            if (TextUtils.isEmpty(string)) {
                return true;
            }
        }
        return false;
    }

    private View initView(LayoutInflater layoutInflater, ViewGroup container) {

        View view = layoutInflater.inflate(R.layout.fragment_sign_in, container, false);
        mNameTv = view.findViewById(R.id.sign_in_user_nameTv);
        mPasswordTv = view.findViewById(R.id.sign_in_passwordTv);
        mSingInBt = view.findViewById(R.id.sign_in_loginBt);

        return view;
    }


}
